//
//  UIButton+GG.m
//  unzip
//
//  Created by yg on 2021/11/21.
//

#import "UIButton+GG.h"
#import <objc/runtime.h>

@implementation UIButton (GG)

+ (UIButton *)gg_customClickWithTitle:(NSString *)title
							titleFont:(UIFont *)titleFont
								image:(UIImage *)image
						   foreground:(UIColor *)foreground
						   background:(id)background{
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button setTitle:title forState:UIControlStateNormal];
	button.titleLabel.font = titleFont;
	[button setImage:image forState:UIControlStateNormal];
	[button setTitleColor:foreground forState:UIControlStateNormal];
	if ([background isKindOfClass:[UIColor class]]) {
//		button.backgroundColor = background;
		[button gg_setBackgroundColor:background forState:UIControlStateNormal];
	}
	if ([background isKindOfClass:[UIImage class]]) {
		[button setBackgroundImage:background forState:UIControlStateNormal];
	}
	return button;
}

+ (UIButton *)gg_customSwitchWithNoSelectedTitle:(NSString *)noSelectedTitle
										 noSelectedImage:(UIImage *)noSelectedImage
									noSelectedTitleColor:(UIColor *)noSelectedTitleColor
										   selectedTitle:(NSString *)selectedTitle
										   selectedImage:(UIImage *)selectedImage
									  selectedTitleColor:(UIColor *)selectedTitleColor
											   titleFont:(UIFont *)titleFont{
	
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button setTitle:noSelectedTitle forState:UIControlStateNormal];
	[button setImage:noSelectedImage forState:UIControlStateNormal];
	[button setTitleColor:noSelectedTitleColor forState:UIControlStateNormal];
	
	[button setTitle:selectedTitle forState:UIControlStateSelected];
	[button setImage:selectedImage forState:UIControlStateSelected];
	[button setTitleColor:selectedTitleColor forState:UIControlStateSelected];
	
	button.titleLabel.font = titleFont;
	
	return button;
}

+ (UIButton *)gg_customSwitchWithNoSelectedTitle:(NSString *)noSelectedTitle
								 noSelectedBGColor:(UIColor *)noSelectedBGColor
							noSelectedTitleColor:(UIColor *)noSelectedTitleColor
								   selectedTitle:(NSString *)selectedTitle
							   selectedBGColor:(UIColor *)selectedBGColor
							  selectedTitleColor:(UIColor *)selectedTitleColor
									   titleFont:(UIFont *)titleFont{
	
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button setTitle:noSelectedTitle forState:UIControlStateNormal];
	[button setBackgroundColor:noSelectedBGColor forState:UIControlStateNormal];
	[button setTitleColor:noSelectedTitleColor forState:UIControlStateNormal];
	
	[button setTitle:selectedTitle forState:UIControlStateSelected];
	[button setBackgroundColor:selectedBGColor forState:UIControlStateSelected];
	[button setTitleColor:selectedTitleColor forState:UIControlStateSelected];
	
	button.titleLabel.font = titleFont;
	
	return button;
}

- (void)gg_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state{
	[self setBackgroundColor:backgroundColor forState:state];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state{
	UIImage *image = [UIButton imageWithColor:backgroundColor];
	[self setBackgroundImage:image forState:state];
}

	///color转image方法
+ (UIImage *)imageWithColor:(UIColor *)color {
	CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
	UIGraphicsBeginImageContext(rect.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetFillColorWithColor(context, [color CGColor]);
	CGContextFillRect(context, rect);
	UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return image;
}
@end

static const void *buttonSpacing = &buttonSpacing;
static const void *buttonPosition = &buttonPosition;
static const void *buttonFlag = &buttonFlag;
@implementation UIButton (GGPosition)

- (void)gg_setPosition:(GGPosition)position spacing:(CGFloat)spacing{
	[self gg_setPosition:position spacing:spacing imageSize:CGSizeMake(self.imageView.image.size.width, self.imageView.image.size.height)];
}
- (void)gg_setPosition:(GGPosition)position spacing:(CGFloat)spacing imageSize:(CGSize)imageSize {
	
	objc_setAssociatedObject(self, buttonFlag, @(YES), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
	objc_setAssociatedObject(self, buttonPosition, @(position), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
	objc_setAssociatedObject(self, buttonSpacing, @(spacing), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
	
	CGFloat imageWith = imageSize.width;
	CGFloat imageHeight = imageSize.height;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
	CGFloat labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
	CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
#pragma clang diagnostic pop
	
	CGFloat imageOffsetX = (imageWith + labelWidth) / 2 - imageWith / 2;//image中心移动的x距离
	CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离
	CGFloat labelOffsetX = (imageWith + labelWidth / 2) - (imageWith + labelWidth) / 2;//label中心移动的x距离
	CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离
	
	switch (position) {
		
		case GGPositionDefault:
			self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
			self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
			break;
		
		case GGPositionImageLeftAndTextRight:
			self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
			self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
			break;
			
		case GGPositionImageRightAndTextLeft:
			self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
			self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2);
			break;
			
		case GGPositionImageTopAndTextBottom:
			self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
			self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
			break;
			
		case GGPositionImageBottomAndTextTop:
			self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
			self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
			break;
			
		default:
			break;
	}
}

- (void)gg_updatePositionLayout{
	GGPosition position = [objc_getAssociatedObject(self, buttonPosition) integerValue];
	CGFloat spacing = [objc_getAssociatedObject(self, buttonSpacing) floatValue];
	[self gg_setPosition:position spacing:spacing];
}


@end
